package com.kugong.util;

import com.jfinal.kit.HttpKit;
import net.sf.json.JSONObject;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2015/8/10.
 */
public class CommonUtil {

    /**
     * 生成比赛组号
     * @param group
     * @param groupNo
     * @param length
     * @return
     */
    public static String makeGroupNo(int group,int groupNo,int length){
        return group + "-" + NumberUtil.FormatNum("" + groupNo, length);
    }

    /**
     * 计算比赛轮次
     * @param playerNum
     * @return
     */
    public static int roundCount(int playerNum){
        int no = 0;
        while (true){
            playerNum = playerNum/2;
            no++;
            if(playerNum==1){

                break;
            }
        }
        return no;
    }

    /**
     * 获取上个月的今天
     * @return
     */
    public static Date lastMonth(){
        Calendar lastDate = Calendar.getInstance();
        lastDate.add(Calendar.MONTH, -1);
        return lastDate.getTime();
    }


    /**
     * 查询斗鱼主播直播状态
     */
    public static String getDouyu(String url) {
        String jsonResult = HttpKit.get(url);

        jsonResult = jsonResult.substring(jsonResult.indexOf("var $ROOM = "),jsonResult.indexOf(";\n</script>\n" +
                "<script type=\"text/javascript\">\n" +
                "var $SYS = {};"));
        jsonResult = jsonResult.substring(12);
        JSONObject ban = JSONObject.fromObject(jsonResult);
        int ban_show_status = ban.getInt("show_status");
        if(ban_show_status==1){
            return "在线";
        }else{
            return "离线";
        }

    }


    /**
     * 查询熊猫主播直播状态
     */
    public static String getPanda(String url) {
        String jsonResult = HttpKit.get(url);
        JSONObject wa = JSONObject.fromObject(jsonResult);
        int wa_show_status = JSONObject.fromObject(JSONObject.fromObject(wa.get("data")).get("videoinfo")).getInt("status");
        if(wa_show_status==2){
            return "在线";
        }else{
            return "离线";
        }
    }

    public static void main(String[] args) {
        String str="";
        try{
            JSONObject ban = JSONObject.fromObject(CommonUtil.getDouyu("101342"));
            JSONObject gou = JSONObject.fromObject(CommonUtil.getDouyu("16789"));
            JSONObject wa = JSONObject.fromObject(CommonUtil.getPanda("10027"));
            int ban_show_status = ban.getInt("show_status");
            int gou_show_status = gou.getInt("show_status");
            int wa_show_status = JSONObject.fromObject(JSONObject.fromObject(wa.get("data")).get("videoinfo")).getInt("status");
            if(ban_show_status==1){
                str+="萌太奇正在直播\n";
            }else{
                str+="萌太奇木有直播\n";
            }
            if(gou_show_status==1){
                str+="安德罗妮正在直播\n";
            }else{
                str+="安德罗妮木有直播\n";
            }
            if(wa_show_status==2){
                str+="瓦娘正在直播\n";
            }else{
                str+="瓦娘木有直播";
            }
        }catch (Exception e){
            e.printStackTrace();
            str = "测试菜单操作";
        }
        System.out.println(str);
    }
}
